$(document).ready(function(){
	vector.loadVector();
	vector.model_cont_load();
});
var vector = {};
(function(res){
	res.model_cont_load=function(){
		//加载库房弹出框
		$.ajax({
			url:"../control/kfDialog.html",
			success:function(data){
				$("#model_cont").append(data);
				$.parser.parse('#model_cont');
			}
		});
		//加载办公场所弹出框
		$.ajax({
			url:"../control/bgcsDialog.html",
			success:function(data){
				$("#model_cont").append(data);
				$.parser.parse('#model_cont');
			}
		});
		//加载料场弹出框
		$.ajax({
			url:"../control/lcDialog.html",
			success:function(data){
				$("#model_cont").append(data);
				$.parser.parse('#model_cont');
			}
		});
		//加载码头弹出框
		$.ajax({
			url:"../control/mtDialog.html",
			success:function(data){
				$("#model_cont").append(data);
				$.parser.parse('#model_cont');
			}
		});
	};
	
	res.loadVector = function(){
		$.ajax({
			url:"../videoFunction/queryVectorGraph.do",
			dataType:"json",
			success:function(data){
				res.constructorGeoJson(data);
			}
		});
	};
	
	res.constructorGeoJson=function(data){
		var polygonArray = new Array();//新数组
		var roundArray = new Array();
		for ( var i in data) {
			if(data[i].graphType=="3"){
				polygonArray.push(res.constructorPolygon(data[i]));//将面数据添加到polygonArray数组中
			};
			if(data[i].graphType=="4"){
				roundArray.push(res.constructorRound(data[i]));
			}
		}
		//console.log(polygonArray);
		L.geoJSON(polygonArray,{
			style:function(feature){
				var style = {};
				style.color = feature.properties.color;
				style.fillColor = feature.properties.fillColor;
				style.fillOpacity = 1;
				return style;
			},
			onEachFeature:function(feature, layer){//添加事件和弹出框
				layer.feature = feature;
				layer.on("click",res.cliceTest);
			}
		}).addTo(map);
		//创建面的geojson绘画
		res.canvasGeoJson(polygonArray,3);
		res.canvasGeoJson(roundArray,4);
	};
	//leaflet原生的geojson绘画方法 type:3面，4圆
	res.canvasGeoJson=function(array,type){
		if (type==3) {
			L.geoJSON(array,{
				style:function(feature){
					var style = {};
					style.color = feature.properties.color;
					style.fillColor = feature.properties.fillColor;
					style.fillOpacity = 1;
					return style;
				},
				onEachFeature:function(feature, layer){
					layer.feature = feature;
					//绑定面的点击事件
					if(feature.properties.modelType!=undefined&&feature.properties.modelType!=""){
						layer.on("dblclick",function(){
							//点击面的事件
							//判断点击的是什么模型，根据模型
							if(feature.properties.modelType=="kf"){
								$("#kf_dialog").find("#modelId").val(feature.properties.id);
								$("#kf_dialog").dialog("open");
							}
							if(feature.properties.modelType=="lc"){
								$("#lc_dialog").find("#modelId").val(feature.properties.id);
								$("#lc_dialog").dialog("open");
							}
							if(feature.properties.modelType=="bgcs"){
								$("#bgcs_dialog").find("#modelId").val(feature.properties.id);
								$("#bgcs_dialog").dialog("open");
							}
							if(feature.properties.modelType=="mt"){
								$("#mt_dialog").find("#modelId").val(feature.properties.id);
								$("#mt_dialog").dialog("open");
							}
						});
					}
					if(feature.properties.showName==0){
						var popup = L.popup().setLatLng(feature.properties.latlng).setContent("<span>"+feature.properties.name+"</span>");
						layer.on("mouseover",function(e){
							//移上的事件
							map.openPopup(popup);
						});
						layer.on("mouseout",function(e){
							//移出的事件
							map.closePopup(popup);
						});
					}
				}
			}).addTo(map);
		}else{
			L.geoJSON(array,{
				pointToLayer:function(feature, latlng){
					return L.circle(latlng,{
						radius: feature.properties.radius,
						color: feature.properties.color, 
						fill: true, fillColor: feature.properties.fillColor,
						fillOpacity: 1});
				},
				onEachFeature:function(feature, layer){
					//绑定面的点击事件
					if(feature.properties.modelType!=undefined&&feature.properties.modelType!=""){
						layer.on("click",function(){
							//点击面的事件
						});
					}
				}
			}).addTo(map);
		}
	};
	//构造圆的GeoJson数据
	res.constructorRound=function(data){
		//设置基本属性
		var properties = {};
		properties.id=data.id;
		properties.graphType=data.graphType;
		properties.name=data.name;
		properties.radius=data.radius;
		properties.yradius=data.radius;
		properties.color=data.color;
		properties.fillColor=data.fillColor;
		properties.weight=data.width;
		properties.isHt = false;
		properties.latlng = [data.reservedLat,data.reservedLog];
		properties.showName = data.showName;
		properties.modelType = data.modelType;
		
		var polyline = {};
		//构造线的坐标数据
		var geometry = {};
		var coordinates = [parseFloat(data.reservedLog),parseFloat(data.reservedLat)];
		geometry.coordinates = coordinates;
		geometry.type = "Point";
		polyline.geometry = geometry;
		polyline.type = "Feature";
		polyline.properties = properties;
		return polyline;
	};
	//构造多边形GeoJson数据
	res.constructorPolygon=function(data){
		//设置基本属性
		var properties = {};
		properties.id=data.id;
		properties.graphType=data.graphType;
		properties.name=data.name;
		properties.radius=data.radius;
		properties.yradius=data.radius;
		properties.color=data.color;
		properties.fillColor=data.fillColor;
		properties.weight=data.width;
		properties.latlng = [data.reservedLat,data.reservedLog];
		properties.modelType = data.modelType;
		properties.showName = data.showName;
		properties.isHt = false;
		var geoJsonData = {};
		geoJsonData.type="Feature";
		geoJsonData.properties = properties;
		
		//创建图形所用的经纬度数组数据
		var coordinates = new Array();
		var polygs = new Array();
		for ( var pol in data.vectorGraphPoints) {
			var polyg = [parseFloat(data.vectorGraphPoints[pol].log),parseFloat(data.vectorGraphPoints[pol].lat)];
			polygs.push(polyg);
		}
		coordinates.push(polygs);
		//创建线面坐标信息
		var geometry = {};
		geometry.coordinates = coordinates;
		geometry.type = "Polygon";
		geoJsonData.geometry = geometry;
		return geoJsonData;
	};
	
	res.constructionData=function(data){
		//构造geoJsonText的外层结构
		var textArray = new Array();
		var geoJsonText = {};
		geoJsonText.type = "FeatureCollection";
		//构造geoJsonPoint的外层结构
		var pointArray = new Array();
		var geoJsonPoint = {};
		geoJsonPoint.type = "FeatureCollection";
		//构造geoJsonLineString的外层结构
		var lineStringArray = new Array();
		var geoJsonLineString = {};
		geoJsonLineString.type = "FeatureCollection";
		//构造geoJsonMultiPolyg的外层结构
		var multiPolygArray = new Array();
		var geoJsonMultiPolyg = {};
		geoJsonMultiPolyg.type = "FeatureCollection";
		for ( var i in data) {
			if(data[i].graphType=="1"){
				//构造文本数据
				textArray.push(res.constrText(data[i]));
			};
			if(data[i].graphType=="2"){
				lineStringArray.push(res.constrLineString(data[i]));
			};
			if(data[i].graphType=="3"){
				multiPolygArray.push(res.constrMultiPolyg(data[i]));
			};
			if(data[i].graphType=="4"){
				pointArray.push(res.constrPoint(data[i]));
			};
		}
		geoJsonPoint.features = pointArray;
		geoJsonLineString.features = lineStringArray;
		geoJsonMultiPolyg.features = multiPolygArray;
		geoJsonText.features = textArray;
		res.loadPolyg(geoJsonLineString);
		res.loadPolyg(geoJsonMultiPolyg);
		res.loadPolyg(geoJsonPoint);
		res.loadPolyg(geoJsonText);
	};
	
	//构造文本数据
	res.constrText=function(data){
		//设置基本属性
		var properties = {};
		properties.id=data.id;
		properties.graphType=data.graphType;
		properties.name=data.name;
		properties.radius=data.radius;
		properties.yradius=data.radius;
		properties.color=data.color;
		properties.fColor=data.fillColor;
		properties.width=data.width;
		properties.isHt = false;
		
		var polyline = {};
		//构造线的坐标数据
		var geometry = {};
		var coordinates = [parseFloat(data.reservedLog),parseFloat(data.reservedLat)];
		geometry.coordinates = coordinates;
		geometry.type = "Text";
		polyline.geometry = geometry;
		polyline.type = "Feature";
		polyline.properties = properties;
		return polyline;
	};
	//构造点型数据
	res.constrPoint=function(data){
		//设置基本属性
		var properties = {};
		properties.id=data.id;
		properties.graphType=data.graphType;
		properties.name=data.name;
		properties.radius=data.radius;
		properties.yradius=data.radius;
		properties.color=data.color;
		properties.fColor=data.fillColor;
		properties.width=data.width;
		properties.isHt = false;
		
		var polyline = {};
		//构造线的坐标数据
		var geometry = {};
		var coordinates = [parseFloat(data.reservedLog),parseFloat(data.reservedLat)];
		geometry.coordinates = coordinates;
		geometry.type = "Point";
		polyline.geometry = geometry;
		polyline.type = "Feature";
		polyline.properties = properties;
		return polyline;
		
	};
	//构造线型数据
	res.constrLineString=function(data){
		
		var polyline = {};
		//设置基本属性
		var properties = {};
		properties.id=data.id;
		properties.graphType=data.graphType;
		properties.name=data.name;
		properties.radius=data.radius;
		properties.color=data.color;
		properties.fColor=data.fillColor;
		properties.width=data.width;
		
		polyline.type = "Feature";
		//构造线的坐标数据
		var geometry = {};
		var coordinates = new Array();
		for ( var pol in data.vectorGraphPoints) {
			var polyg = [parseFloat(data.vectorGraphPoints[pol].log),parseFloat(data.vectorGraphPoints[pol].lat)];
			coordinates.push(polyg);
		}
		
		geometry.coordinates = coordinates;
		geometry.type = "LineString";
		polyline.geometry = geometry;
		polyline.properties = properties;
		return polyline;
	};
	//构造多边形数据
	res.constrMultiPolyg=function(data){
		var conData = {};
		conData.type="Feature";
		//设置基本属性
		var properties = {};
		properties.id=data.id;
		properties.graphType=data.graphType;
		properties.name=data.name;
		properties.radius=data.radius;
		properties.color=data.color;
		properties.fColor=data.fillColor;
		properties.width=data.width;
		
		conData.properties = properties;
		
		//创建图形所用的经纬度数组数据
		var coordinates = new Array();
		var polygs = new Array();
		for ( var pol in data.vectorGraphPoints) {
			var polyg = [parseFloat(data.vectorGraphPoints[pol].log),parseFloat(data.vectorGraphPoints[pol].lat)];
			polygs.push(polyg);
		}
		coordinates.push(polygs);
		//创建线面坐标信息
		var geometry = {};
		geometry.coordinates = coordinates;
		geometry.type = "Polygon";
		conData.geometry = geometry;
		return conData;
	};
	res.loadPolyg=function(geoJson){
		console.log(geoJson)
		var canvasLayer = new L.CanvasGeojsonLayer({
		     onMouseOver :function(features){
		         //处理鼠标悬停事件 
		    },
		    onMouseOut  : function(features){
		         //处理mouseout事件
		    },
		    onClick  : function(features){
		         //处理鼠标点击事件
		    }
		});
		
		canvasLayer.addTo(map);
		var data = L.CanvasFeatureFactory(geoJson);
		canvasLayer.addCanvasFeatures(data);
		canvasLayer.render();
	};
})(vector);
